/**
 * Created by jiexin on 2017/8/25.
 */

layui.use(['form', 'element', 'layer'], function() {
    var form = layui.form;
    var element = layui.element;
    var $ = layui.jquery;
    var layer = layui.layer; //弹层
    console.log(module_name);

    var background_state = 0;


    form.on('checkbox(notag)', function(data){
        // console.log(data.elem); //得到checkbox原始DOM对象
        // console.log(data.elem.checked); //是否被选中，true或者false
        // console.log(data.value); //复选框value值，也可以通过data.elem.value得到
        // console.log(data.othis); //得到美化后的DOM对象

        if (data.elem.checked === true){
            $("#versionnum").addClass('layui-btn-disabled');

            $("#tagmessage").addClass('layui-btn-disabled')
        }else{
            $("#versionnum").removeClass('layui-btn-disabled');
            $("#tagmessage").removeClass('layui-btn-disabled')
        }
    });

    form.on('submit(submitgit)', function(data) {
        // console.log(data.elem); //被执行事件的元素DOM对象，一般为button对象
        //console.log(data.field); //当前容器的全部表单字段，名值对形式：{name: value}
        var formdata = data.field;
        console.log(formdata);
        var senddata = {};

        if (formdata["notag"] === 'on') {
            senddata["module"] = module_name;
            senddata["commit_message"] = formdata["commitmessage"];
        }else{
            senddata["module"] = module_name;
            senddata["tag_version"] = formdata["versionnum"];
            senddata["commit_message"] = formdata["commitmessage"];
            senddata["tag_message"] = formdata["tagmessage"];
        }
        console.log(senddata);
        gitsend(senddata);
        // layer_close();
        return false;
    });

    function gitsend(senddata) {
        $.ajax({
            url: "/gitsend/",
            dataType: 'json',
            type: 'POST',
            data: {"data": JSON.stringify(senddata)},
            success: function (returndedata) {
                console.log(returndedata);
                background_state = 0;
                layer.msg(returndedata["status"], {
                    time: 20000,
                    //20s后自动关闭
                    btn: ['确定'],
                    success: function (layero) {
                        var btn = layero.find('.layui-layer-btn');
                        var btntrue = layero.find('.layui-layer-btn0');
                        btn.css('text-align', 'center');
                    }
                })
            }
        });
    }

    form.verify({
        // textbox: function(value){ //value：表单的值、item：表单的DOM对象
        //     if(value.length < 7){
        //         return '标题至少得7个字符啊';
        //     }
        // }
        textbox: [/[v][0-9]\.[0-9]$/, '版本号格式不正确，例如：v1.0']
    });

    $('.demoTable .layui-btn').on('click', function() {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});




